<?php

namespace App\Http\Controllers\Steward;

use App\Models\Goods\GoodsMarktingStrategy;
use App\Models\Goods\GoodsSale;
use App\Models\Goods\GoodsMaster;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis as Redis;
use Maatwebsite\Excel\Facades\Excel;


class PriceController extends Controller
{

    //价格列表
    public function index(Request $request)
    {

        return view('steward/goods/price/index');
    }

    //价格列表数据查询
    public function search( Request $request)
    {
        $rp      = $request->input('rp',10);
        $offset=$request->input('offset',0);

        $mallID    =Redis::get('WDH_STEWARD_MALL_ID_' . session_id());

            $where=[];
            //商家编码
            if($request->input('product_code')){
                $where[] = ['product_code',$request->input('product_code')];
            }
            //商品名称
            if($request->input('goodsName')){
                $where[] = ['goodsName','like','%'.$request->input('goodsName').'%'];
            }

            //商品价格
            if($request->input('min_price')   ){
                $where[] = ['salePrice','>=',$request->input('min_price')];

            }
            if( $request->input('max_price')){
                $where[] = ['salePrice','<=',$request->input('max_price')];
            }

            //查询价格信息
            $goodsDatas =GoodsMarktingStrategy::
                where(['shop_id'=>$mallID])
                ->where($where)
                ->offset($offset)
                ->limit($rp)
                ->get()
                ->toArray();

            $goodsData=GoodsMarktingStrategy::where(['shop_id'=>$mallID])->where($where)->get()->toArray();
                $total=count($goodsData);

            if(!empty($goodsDatas))
            {
                foreach($goodsDatas as $value){
                    if($value['goodsName']!='')
                    {
                        $return_data[]=array(
                            'goodsName'=>$value['goodsName'],//商品名称
                            'code_info'=>$value['product_code'],//商家编码 条形码
                            'spec'=>$value['product_spec'],//商品规格
                            'salePrice'=>'￥'.'<span id="'.$value["product_code"].'">'.$value['salePrice'].'&nbsp;<a href="javascript:void(0)" class="price" data_type="'.$value['salePrice'].'" data_id="' . $value['product_code']. '"><span class="glyphicon glyphicon-pencil"></span></a></span>',
                            'timeStamp'=>'<span id="t_'.$value['product_code'].'">'.$value['timeStamp'].'</span>'
                        );
                    }
                }

                $result_data=array(
                    'total'=>$total,
                    'rows'=>$return_data
                );
            }else{
                $result_data=array(
                    'total'=>$total,
                    'rows'=>[]
                );
            }
            return $result_data;



    }



    //价格修改
    public function update( Request $request)
    {
        $product_code=$request->input('product_code');
        $price=$request->input('price');

        //修改价格
        $time=Carbon::now();
        $res= $goods_data= GoodsMarktingStrategy::where('product_code',$product_code)->update(['salePrice'=>$price,'timeStamp'=>$time ]);
        if($res)
        {
            return response()->json(['code'=>200, 'message'=>'价格修改成功','data'=>$time]);
        }
    }


    /**
     * 价格批量上传
     */
    public function batchPrice(){

        return view('steward/goods/batch/price');

    }

    /**
     * 库存模板导出
     */
    public function export(){

        return response()->download(resource_path().'/goods_price.xls', '价格上传模板.xls');
    }

    /**
     * 价格批量添加
     */
    public function batchDo(Request $request)
    {

        $mall_id = Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) ? Redis::get('WDH_STEWARD_MALL_ID_' . session_id()) : 0;

        $url = 'public' . $request->input('file_url');
        $count_success = 0;
        $count_fail = 0;

        Excel::load($url, function ($reader) use($mall_id,&$count_success,&$count_fail){

            $data = $reader->getSheet();

            if($data){

                $data = $data->toArray();
                unset($data[0]);
            }

            foreach ($data as $item){

                if(empty($item[0]) || empty($item[1]) || empty($item[2]) ){

                    $count_fail ++;
                    continue;
                }

                $p_id = (int)$item[0];
                $marketPrice = (int)$item[1];
                $salePrice = (int)$item[2];

                $res = GoodsMarktingStrategy::where('shop_id',$mall_id)->where('postID',$p_id)->update(['marketPrice' => $marketPrice,'salePrice' => $salePrice]);
                if($res){

                    $count_success++;
                }else{
                    $count_fail++;
                }
            }
        });

        unlink(public_path().$request->input('file_url'));

        return response()->json(['code' => 200,'message' => $count_success.'个商品操作成功,'.$count_fail.'个商品操作失败']);
    }
}